package com.wc.算法提高课.A第一章_动态规划.区间DP.棋盘分割;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/12 19:38
 * @description https://www.acwing.com/problem/content/description/323/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 15, M = 9, INF = (int) 1e9;
    //f[x1][y1][x2][y2][k] 代表从x1 y1到x2 y2分了k分的最小军方差
    static double[][][][][] f = new double[M][M][M][M][N];
    static double[][] s = new double[M][M];
    static int n, m = 8;
    static double X = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= 8; i++) {
            for (int j = 1; j <= 8; j++) {
                s[i][j] = sc.nextInt();
                s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
            }
        }

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                for (int k = 1; k <= m; k++) {
                    for (int l = 1; l <= m; l++) {
                        Arrays.fill(f[i][j][k][l], -1);
                    }
                }
            }
        }

        X = s[m][m] / n;
        out.printf("%.3f\n", Math.sqrt(dp(1, 1, 8, 8, n)));
        out.flush();
    }

    static double dp(int x1, int y1, int x2, int y2, int k) {
        if (k == 1) return f[x1][y1][x2][y2][k] = get(x1, y1, x2, y2);
        if (f[x1][y1][x2][y2][k] >= 0) return f[x1][y1][x2][y2][k];
        f[x1][y1][x2][y2][k] = INF;
        // 分割,横着
        for (int i = x1; i < x2; i++) {
            f[x1][y1][x2][y2][k] = Math.min(f[x1][y1][x2][y2][k],
                    dp(x1, y1, i, y2, k - 1) + get(i + 1, y1, x2, y2));
            f[x1][y1][x2][y2][k] = Math.min(f[x1][y1][x2][y2][k],
                    dp(i + 1, y1, x2, y2, k - 1) + get(x1, y1, i, y2));
        }
        // 竖着
        for (int i = y1; i < y2; i++) {
            f[x1][y1][x2][y2][k] = Math.min(f[x1][y1][x2][y2][k],
                    dp(x1, y1, x2, i, k - 1) + get(x1, i + 1, x2, y2));
            f[x1][y1][x2][y2][k] = Math.min(f[x1][y1][x2][y2][k],
                    dp(x1, i + 1, x2, y2, k - 1) + get(x1, y1, x2, i));
        }
        return f[x1][y1][x2][y2][k];
    }

    static double get(int x1, int y1, int x2, int y2) {
        double sum = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1] - X;
        return sum * sum / n;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
